<template>
	<div class="system-dict-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入字典名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddDict">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增字典
				</el-button>
			</div>
			<el-table :data="tableData.data" style="width: 100%">
				<el-table-column prop="id" label="编号" width="100" />
				<el-table-column prop="name" label="字典名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="type" label="字典类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="字典状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 0">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="字典备注" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ formatPast(scope.row.createTime, 'YYYY-mm-dd') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" type="text" @click="onOpenEditDict(scope.row)">修改</el-button>
						<el-button size="small" type="text" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="tableData.param.pageNo"
				background
				v-model:page-size="tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<AddDict ref="addDictRef" />
		<EditDict ref="editDictRef" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import AddDict from '/@/views/system/dict/component/addDict.vue';
import EditDict from '/@/views/system/dict/component/editDict.vue';
import { useDictApi } from '/@/api/system/dict';
import { formatPast } from '/@/utils/formatTime';

export default defineComponent({
	name: 'systemDict',
	components: { AddDict, EditDict },
	setup() {
		const dictApi = useDictApi();
		const addDictRef = ref();
		const editDictRef = ref();
		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNo: 1,
					pageSize: 10,
				},
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			state.tableData.loading = true;
			dictApi.listData(state.tableData.param).then((response) => {
				if (response.data) {
					state.tableData.data = response.data.list;
					state.tableData.total = response.data.total;
					state.tableData.loading = false;
				}
			});
		};
		// 打开新增字典弹窗
		const onOpenAddDict = () => {
			addDictRef.value.openDialog();
		};
		// 打开修改字典弹窗
		const onOpenEditDict = (row: any) => {
			editDictRef.value.openDialog(row);
		};
		// 删除字典
		const onRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久删除字典名称：“${row.name}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					ElMessage.success('删除成功');
				})
				.catch(() => {});
		};
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
			dictApi.listData(state.tableData.param).then((response) => {
				if (response.data) {
					state.tableData.data = response.data.list;
					state.tableData.total = response.data.total;
					state.tableData.loading = false;
				}
			});
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNo = val;
			dictApi.listData(state.tableData.param).then((response) => {
				if (response.data) {
					state.tableData.data = response.data.list;
					state.tableData.total = response.data.total;
					state.tableData.loading = false;
				}
			});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			addDictRef,
			editDictRef,
			onOpenAddDict,
			onOpenEditDict,
			onRowDel,
			onHandleSizeChange,
			onHandleCurrentChange,
			formatPast,
			...toRefs(state),
		};
	},
});
</script>
